package com.houtai.service.activity.works;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.houtai.base.ResultVo;
import com.houtai.bean.*;
import com.houtai.bean.dto.avtivity.works.TdActivityWorksDetailDto;
import com.houtai.bean.dto.avtivity.works.TdActivityWorksInsertDto;
import com.houtai.bean.dto.avtivity.works.TdActivityWorksListDto;
import com.houtai.bean.dto.avtivity.works.comments.TdActivityWorksCommentsInsertDto;
import com.houtai.bean.dto.avtivity.works.comments.TdActivityWorksCommentsListDto;
import com.houtai.bean.dto.praise.PraiseDto;
import com.houtai.bean.vo.activity.TdActivityDetail;
import com.houtai.bean.vo.activity.works.*;
import com.houtai.bean.vo.activity.works.comments.TdActivityWorksCommentsList;
import com.houtai.bean.vo.activity.works.comments.TdActivityWorksCommentsListVo;
import com.houtai.bean.vo.activity.works.comments.TdActivityWorksCommentsReplyList;
import com.houtai.common.util.WordsUtil;
import com.houtai.dao.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ActivityWorksService {
    private static Logger logger = Logger.getLogger(ActivityWorksService.class);
    @Autowired
    private TdActivityDao tdActivityDao;
    @Autowired
    private TdActivityWorksDao tdActivityWorksDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TdActivityApplyDao tdActivityApplyDao;
    @Autowired
    private TsJzDictDao tsJzDictDao;
    @Autowired
    private TdActivityWorksCommentsDao tdActivityWorksCommentsDao;
    @Autowired
    private TdPraiseDao tdPraiseDao;
    /**
     * @Purpose:    添加作品
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public ResultVo insertActivityWorks(TdActivityWorksInsertDto dto, String token) {
        String logInfo = this.getClass().getName() + ":insertActivityWorks:";
        ResultVo retInfo = new ResultVo();
        logger.info("======" + logInfo + "begin======");
        try {
            User user = (User)redisTemplate.opsForValue().get(token);
            if (dto.getActivity_id() != 0) {
                TdActivityDetail tdActivityDetail = tdActivityDao.findTdActivityDetail(dto.getActivity_id());
                if (tdActivityDetail != null) {

                    boolean flag = false;
                    if (dto.getActivity_id() > 0) {
                        TdActivityApply tdActivityApply = tdActivityApplyDao.findTdActivityApplyId(user.getUser_id(), dto.getActivity_id());
                        if (tdActivityApply != null) {
                            if (tdActivityDetail.getStart_time() <= new Date().getTime()
                                    && new Date().getTime() <= tdActivityDetail.getEnd_time()) {
                                flag = true;
                            } else {
                                retInfo.setMark("1");
                                retInfo.setTip("活动已结束或未开始");
                            }

                        } else {
                            retInfo.setMark("1");
                            retInfo.setTip("未报名该活动");
                        }
                    } else {
                        flag = true;
                    }
                    if (flag) {
                        TsJzDict tsJzDict = tsJzDictDao.findDictById(dto.getWorks_type());
                        if (tsJzDict != null) {

                            if (dto.getWorks_img().contains(".jpg") || dto.getWorks_img().contains(".png")
                                    || dto.getWorks_img().contains(".PNG") || dto.getWorks_img().contains(".JPG") ) {
                                if (dto.getWorks_video().equals("")) {
                                    TdActivityWorks tdActivityWorks = new TdActivityWorks();
                                    tdActivityWorks.setActivity_id(dto.getActivity_id());
                                    tdActivityWorks.setUser_id(user.getUser_id());
                                    tdActivityWorks.setWorks_content(dto.getWorks_content());
                                    tdActivityWorks.setWorks_img(dto.getWorks_img());
                                    tdActivityWorks.setWorks_label(dto.getWorks_label());
                                    tdActivityWorks.setWorks_name(dto.getWorks_name());
                                    tdActivityWorks.setWorks_type(dto.getWorks_type());
                                    tdActivityWorks.setWorks_video(dto.getWorks_video());
                                    tdActivityWorksDao.insertTdActivityWorks(tdActivityWorks);
                                    retInfo.setMark("0");
                                    retInfo.setTip("添加作品成功");
                                } else {
                                    if (dto.getWorks_video().contains(".mp4")) {
                                        TdActivityWorks tdActivityWorks = new TdActivityWorks();
                                        tdActivityWorks.setActivity_id(dto.getActivity_id());
                                        tdActivityWorks.setUser_id(user.getUser_id());
                                        tdActivityWorks.setWorks_content(dto.getWorks_content());
                                        tdActivityWorks.setWorks_img(dto.getWorks_img());
                                        tdActivityWorks.setWorks_label(dto.getWorks_label());
                                        tdActivityWorks.setWorks_name(dto.getWorks_name());
                                        tdActivityWorks.setWorks_type(dto.getWorks_type());
                                        tdActivityWorks.setWorks_video(dto.getWorks_video());
                                        tdActivityWorksDao.insertTdActivityWorks(tdActivityWorks);
                                        retInfo.setMark("0");
                                        retInfo.setTip("添加作品成功");
                                    } else {
                                        retInfo.setMark("1");
                                        retInfo.setTip("视频必须是mp4格式");
                                    }
                                }

                            } else {
                                retInfo.setMark("1");
                                retInfo.setTip("图片必须是jpg或者pngG格式");
                            }


                        } else {
                            retInfo.setMark("1");
                            retInfo.setTip("作品类型不存在");
                        }

                    }

                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("活动不存在");
                }
            } else {
                TsJzDict tsJzDict = tsJzDictDao.findDictById(dto.getWorks_type());
                if (tsJzDict != null) {
                    TdActivityWorks tdActivityWorks = new TdActivityWorks();
                    tdActivityWorks.setActivity_id(dto.getActivity_id());
                    tdActivityWorks.setUser_id(user.getUser_id());
                    tdActivityWorks.setWorks_content(dto.getWorks_content());
                    tdActivityWorks.setWorks_img(dto.getWorks_img());
                    tdActivityWorks.setWorks_label(dto.getWorks_label());
                    tdActivityWorks.setWorks_name(dto.getWorks_name());
                    tdActivityWorks.setWorks_type(dto.getWorks_type());
                    tdActivityWorks.setWorks_video(dto.getWorks_video());
                    tdActivityWorksDao.insertTdActivityWorks(tdActivityWorks);
                    retInfo.setMark("0");
                    retInfo.setTip("添加作品成功");

                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("作品类型不存在");
                }
            }


        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    作品列表
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     UserActiviTdActivityWorksListVotyListVo
     */
    public TdActivityWorksListVo activityWorksList(TdActivityWorksListDto dto) {
        String logInfo = this.getClass().getName() + ":activityWorksList:";
        TdActivityWorksListVo retInfo = new TdActivityWorksListVo();
        logger.info("======" + logInfo + "begin======");
        try {
            String orderBy = "works_id DESC";
            PageHelper.startPage(dto.getCurrent_page(), dto.getPage_size(), orderBy);
            List<TdActivityWorksList> lists = tdActivityWorksDao.findTdActivityWorksList(dto);
            PageInfo<TdActivityWorksList> list = new PageInfo<>(lists);
            if (list.getTotal() > 0) {
                retInfo.setCurrent_page(dto.getCurrent_page());
                retInfo.setList(list.getList());
                retInfo.setPage_size(list.getPageSize());
                retInfo.setTotal_record(list.getTotal());
                retInfo.setPages(list.getPages());
                retInfo.setMark("0");
                retInfo.setTip("查询成功");
            } else {
                retInfo.setMark("1");
                retInfo.setTip("无作品列表");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    作品评论
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public ResultVo activityWorksComments(TdActivityWorksCommentsInsertDto dto, String token) {
        String logInfo = this.getClass().getName() + ":activityWorksComments:";
        ResultVo retInfo = new ResultVo();
        logger.info("======" + logInfo + "begin======");
        try {
            TdActivityWorksDetail tdActivityWorksDetail = tdActivityWorksDao.findTdActivityWorksDetail(dto.getWorks_id());
            if (tdActivityWorksDetail != null) {
                if (WordsUtil.isExit(dto.getComments_content())) {
                    User user = (User)redisTemplate.opsForValue().get(token);
                    TdActivityWorksComments comments = new TdActivityWorksComments();
                    comments.setWorks_id(dto.getWorks_id());
                    comments.setComments_content(dto.getComments_content());
                    comments.setIs_reply(dto.getComments_id());
                    comments.setUser_id(user.getUser_id());
                    tdActivityWorksCommentsDao.insertTdActivityWorksComments(comments);
                    if (dto.getComments_id() == 0) {
                        tdActivityWorksDetail.setComments_size(tdActivityWorksDetail.getComments_size() + 1);
                        tdActivityWorksDao.updateTdActivityWorksCommentsSize(tdActivityWorksDetail);
                    }

                    retInfo.setMark("0");
                    retInfo.setTip("成功");
                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("评论包含敏感词");
                }
            } else {
                retInfo.setMark("1");
                retInfo.setTip("活动不存在");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    作品评论列表
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ArticleListVo
     */
    public TdActivityWorksCommentsListVo activityWorksCommentsList(TdActivityWorksCommentsListDto dto) {
        String logInfo = this.getClass().getName() + ":activityWorksCommentsList:";
        TdActivityWorksCommentsListVo retInfo = new TdActivityWorksCommentsListVo();
        logger.info("======" + logInfo + "begin======");
        try {

            PageHelper.startPage(dto.getCurrent_page(), dto.getPage_size(), "comments_id DESC");
            List<TdActivityWorksCommentsList> lists = tdActivityWorksCommentsDao.findTdActivityWorksCommentsList(dto);
            PageInfo<TdActivityWorksCommentsList> list = new PageInfo<>(lists);
            if (list.getTotal() > 0) {
                for (int i = 0; i < list.getList().size(); i ++){
                    List<TdActivityWorksCommentsReplyList> reply = tdActivityWorksCommentsDao.findTdActivityWorksCommentsReplyList(list.getList().get(i).getComments_id());
                    list.getList().get(i).setReplys(reply);
                }

                retInfo.setCurrent_page(dto.getCurrent_page());
                retInfo.setList(list.getList());
                retInfo.setPage_size(list.getPageSize());
                retInfo.setTotal_record(list.getTotal());
                retInfo.setPages(list.getPages());
                retInfo.setMark("0");
                retInfo.setTip("查询成功");
            } else {
                retInfo.setMark("1");
                retInfo.setTip("无评论");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    作品详情
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     TdActivityWorksDetailVo
     */
    public TdActivityWorksDetailVo activityWorksDetail(TdActivityWorksDetailDto dto, String token) {
        String logInfo = this.getClass().getName() + ":activityWorksDetail:";
        TdActivityWorksDetailVo retInfo = new TdActivityWorksDetailVo();
        logger.info("======" + logInfo + "begin======");
        try {
            TdActivityWorksDetail tdActivityWorksDetail = tdActivityWorksDao.findTdActivityWorksDetail(dto.getWorks_id());
            if (tdActivityWorksDetail != null) {
                if (redisTemplate.opsForValue().get(token) != null) {
                    User user = (User)redisTemplate.opsForValue().get(token);
                    TdPraise tdPraise = tdPraiseDao.findTdPraise(dto.getWorks_id(), user.getUser_id());
                    if (tdPraise != null && tdPraise.getState() == 0) {
                        tdActivityWorksDetail.setIs_praise(0);
                    }
                }

                List<TdActivityWorksDetailList> list = tdActivityWorksDao.findTdActivityWorksDetailList(dto.getWorks_id(), tdActivityWorksDetail.getUser_id());
                retInfo.setDetail(tdActivityWorksDetail);
                retInfo.setList(list);
                retInfo.setMark("0");
                retInfo.setTip("查询成功");
            } else {
                retInfo.setMark("1");
                retInfo.setTip("作品不存在");
            }


        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    作品点赞
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     TdActivityWorksDetailVo
     */
    public synchronized ResultVo praise(PraiseDto dto, String token) {
        String logInfo = this.getClass().getName() + ":praise:";
        ResultVo retInfo = new ResultVo();
        logger.info("======" + logInfo + "begin======");
        try {
            TdActivityWorksDetail tdActivityWorksDetail = tdActivityWorksDao.findTdActivityWorksDetail(dto.getWorks_id());
            if (tdActivityWorksDetail != null) {

                User user = (User)redisTemplate.opsForValue().get(token);
                TdPraise tdPraise = tdPraiseDao.findTdPraise(dto.getWorks_id(), user.getUser_id());

                if (dto.getState() == 0) {
                    if (tdPraise != null ) {
                        if (tdPraise.getState() == 1) {
                            tdPraise.setState(0);
                            tdPraiseDao.updateTdPraise(tdPraise);
                            tdActivityWorksDetail.setPraise_size(tdActivityWorksDetail.getPraise_size() + 1);
                            tdActivityWorksDao.updateTdActivityWorksCommentsSize(tdActivityWorksDetail);
                            retInfo.setMark("0");
                            retInfo.setTip("成功");
                        } else {
                            retInfo.setMark("1");
                            retInfo.setTip("已点赞");
                        }
                    } else {
                        tdPraise = new TdPraise();
                        tdPraise.setUser_id(user.getUser_id());
                        tdPraise.setWorks_id(dto.getWorks_id());
                        tdPraiseDao.insertTdPraise(tdPraise);
                        tdActivityWorksDetail.setPraise_size(tdActivityWorksDetail.getPraise_size() + 1);
                        tdActivityWorksDao.updateTdActivityWorksCommentsSize(tdActivityWorksDetail);
                        retInfo.setMark("0");
                        retInfo.setTip("成功");
                    }

                } else {
                    if (tdPraise != null ) {
                        if (tdPraise.getState() == 1) {
                            retInfo.setMark("1");
                            retInfo.setTip("已取消点赞");
                        } else {
                            tdPraise.setState(1);
                            tdPraiseDao.updateTdPraise(tdPraise);
                            tdActivityWorksDetail.setPraise_size(tdActivityWorksDetail.getPraise_size() - 1);
                            tdActivityWorksDao.updateTdActivityWorksCommentsSize(tdActivityWorksDetail);
                            retInfo.setMark("0");
                            retInfo.setTip("成功");
                        }
                    } else {
                        retInfo.setMark("1");
                        retInfo.setTip("不可取消点赞");
                    }
                }


            } else {
                retInfo.setMark("1");
                retInfo.setTip("作品不存在");
            }


        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
